__type__ = "html"
__instance__ = "PyBtHtml"

import urllib
import urllib2
import urlparse
import logging
import re

# this way we don't force users to install bs incase they do not want to use module http
soup_present = True
soup_err = "Module source_html requires BeautifulSoup. Please install it from http://www.crummy.com/software/BeautifulSoup/ or from your distribution repository."

try:
    from BeautifulSoup import BeautifulSoup
except:
    logging.warning(soup_err)
    soup_present = False

class PyBtHtml:
    """
        Parse all links from html-page and search patterns from the link url and name.
    """

    def get_filename(self, href):
        return href[href.rfind("/")+1:]

    def run(self, source, patterns, config, session):
        if not soup_present:
            logging.error(soup_err)
            return

        matches = []
        page = urllib2.urlopen(config['url'])
        soup = BeautifulSoup(page)
        for link in soup.findAll('a'):
            if not link.has_key("href"): continue
            title = link.string
            url = link['href']

            # fix broken urls
            if url.startswith('//'):
                url = "http:" + url
            elif url.startswith('/'):
                url = urlparse.urljoin(config['url'], url)
                
            url_readable = urllib.unquote(url)

            for pattern in patterns:
                regexp = str(pattern['pattern'])
                # check if pattern matches url_readable, url or title
                if re.search(regexp, url_readable, re.IGNORECASE|re.UNICODE) or re.search(regexp, url, re.IGNORECASE|re.UNICODE) or re.search(regexp, title, re.IGNORECASE|re.UNICODE):
#                    logging.debug("Found url %s" % url_readable)
                    match = pattern.copy()
                    match['torrent'] = url
                    full_title = "%s - %s" % (self.get_filename(url_readable), title)
                    # incase title contains xxxxxxx.torrent - foooo.torrent clean it a bit (get upto first .torrent)
                    if (full_title.lower().find('.torrent')):
                        full_title = full_title[:full_title.lower().find(".torrent")]
                    match['title'] = full_title
                    matches.append(match)
            
        return matches
